﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows8Accelerator.Core.Xaml.Helpers;

namespace Windows8Accelerator.Core.Xaml.Controls
{
    public class SuperListViewItemContainer : Grid
    {
        public static readonly DependencyProperty HasBeenBoundedProperty = DependencyProperty.RegisterAttached(
          "HasBeenBounded",
          typeof(Boolean),
          typeof(SuperListViewItemContainer),
          new PropertyMetadata(false)
        );
        public static void SetHasBeenBounded(UIElement element, Boolean value)
        {
          element.SetValue(HasBeenBoundedProperty, value);
        }
        public static Boolean GetHasBeenBounded(UIElement element)
        {
            return (Boolean)element.GetValue(HasBeenBoundedProperty);
        }

        public SuperListViewItemContainer()
        {
            Loaded += SuperListViewItemContainer_Loaded;
        }

        void SuperListViewItemContainer_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var variableSizedWrapGrid = this.GetParentOf<VariableSizedWrapGrid>();
            if (variableSizedWrapGrid != null && !GetHasBeenBounded(variableSizedWrapGrid))
            {
                var superListView = this.GetParentOf<SuperListView>();
                if (superListView != null)
                {
                    variableSizedWrapGrid.SetBinding(VariableSizedWrapGrid.ItemWidthProperty,
                        new Binding()
                        {
                            Path = new PropertyPath("ActualItemWidth"),
                            Source = superListView
                        });
                    variableSizedWrapGrid.SetBinding(VariableSizedWrapGrid.ItemHeightProperty,
                        new Binding()
                        {
                            Path = new PropertyPath("ActualItemHeight"),
                            Source = superListView
                        });
                    if (superListView.SemanticZoomOwner == null || superListView.IsZoomedInView)
                    {
                        variableSizedWrapGrid.SetBinding(VariableSizedWrapGrid.MarginProperty,
                            new Binding()
                            {
                                Path = new PropertyPath("ActualGroupMargin"),
                                Source = superListView,
                            });
                    }
                    else
                    {
                        variableSizedWrapGrid.SetBinding(VariableSizedWrapGrid.MarginProperty,
                           new Binding()
                           {
                               Path = new PropertyPath("ActualPanelMargin"),
                               Source = superListView,
                           });
                    }
                    SetHasBeenBounded(variableSizedWrapGrid, true);
                }
            }
        }
    }


}
